package cn.szabadsag.quartz.job;

import cn.szabadsag.base.constant.JobConstants;
import cn.szabadsag.order.service.IOrderAdoptService;
import cn.szabadsag.order.service.IProductOrderService;
import cn.szabadsag.quartz.domain.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 统一job
 */
public class MainJob  implements Job{

    @Autowired
    private IOrderAdoptService orderAdoptService;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //根据类型不同做不同处理
        QuartzJobInfo info = (QuartzJobInfo) context.getJobDetail()
                .getJobDataMap().get("params");

        byte type = info.getType();
        switch(type){ //魔鬼数字
            case JobConstants.JOB_TYPE_ADOPT_ORDER_CANCEL:
                //领养订单取消
                Map<String, Object> params = info.getParams();
                Object orderIdObj = params.get("orderId");
                if (orderIdObj!=null){
                    Long orderId = Long.valueOf(orderIdObj.toString());
                    orderAdoptService.cancelByQuartz(orderId);
                }
                break;
            case JobConstants.JOB_TYPE_PRODUCT_ORDER_CANCEL:
                //服务订单取消
                Map<String, Object> params1 = info.getParams();
                Object orderIdObj1 = params1.get("orderId");
                if (orderIdObj1!=null){
                    Long orderId1 = Long.valueOf(orderIdObj1.toString());
                    productOrderService.cancelByQuartz(orderId1);
                }
                break;
            case JobConstants.JOB_TYPE_ADOPT_ORDER_CONFIRM:
                //领养订单确认
                break;
                //@TODO 其他自己玩
        }

    }
}
